package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Storelist;
import com.ruoyi.system.domain.dto.ScenicSpotsDto;
import com.ruoyi.system.mapper.StorelistMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ScenicSpotsMapper;
import com.ruoyi.system.domain.ScenicSpots;
import com.ruoyi.system.service.IScenicSpotsService;

/**
 * 景点信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-18
 */
@Service
public class ScenicSpotsServiceImpl implements IScenicSpotsService 
{
    @Autowired
    private ScenicSpotsMapper scenicSpotsMapper;

    @Autowired
    private StorelistMapper storelistMapper;

    /**
     * 查询景点信息
     * 
     * @param spotId 景点信息主键
     * @return 景点信息
     */
    @Override
    public ScenicSpotsDto selectScenicSpotsBySpotId(Integer spotId)
    {
        ScenicSpots scenicSpots = scenicSpotsMapper.selectScenicSpotsBySpotId(spotId);
        List<Storelist> storeList = storelistMapper.selectStorelistBySpotsId(scenicSpots.getSpotId());


        ScenicSpotsDto scenicSpotsDto = new ScenicSpotsDto();
        BeanUtils.copyProperties(scenicSpots, scenicSpotsDto);

        scenicSpotsDto.setScenicSpotsList(storeList);
        return scenicSpotsDto;
    }

    /**
     * 查询景点信息列表
     * 
     * @param scenicSpots 景点信息
     * @return 景点信息
     */
    @Override
    public List<ScenicSpotsDto> selectScenicSpotsList(ScenicSpots scenicSpots)
    {
        List<ScenicSpots> scenicSpotsList = scenicSpotsMapper.selectScenicSpotsList(scenicSpots);
        List<ScenicSpotsDto> resultDto = new ArrayList<>();

        for (ScenicSpots item : scenicSpotsList) {

            ScenicSpotsDto scenicSpotsDto = new ScenicSpotsDto();

            BeanUtils.copyProperties(item, scenicSpotsDto);

            List<Storelist> storeList = storelistMapper.selectStorelistBySpotsId(item.getSpotId());
            scenicSpotsDto.setScenicSpotsList(storeList);
            resultDto.add(scenicSpotsDto);
        }

        return resultDto;
    }

    /**
     * 新增景点信息
     * 
     * @param scenicSpots 景点信息
     * @return 结果
     */
    @Override
    public int insertScenicSpots(ScenicSpots scenicSpots)
    {
        scenicSpots.setCreateTime(DateUtils.getNowDate());
        return scenicSpotsMapper.insertScenicSpots(scenicSpots);
    }

    /**
     * 修改景点信息
     * 
     * @param scenicSpots 景点信息
     * @return 结果
     */
    @Override
    public int updateScenicSpots(ScenicSpots scenicSpots)
    {
        scenicSpots.setUpdateTime(DateUtils.getNowDate());
        return scenicSpotsMapper.updateScenicSpots(scenicSpots);
    }

    /**
     * 批量删除景点信息
     * 
     * @param spotIds 需要删除的景点信息主键
     * @return 结果
     */
    @Override
    public int deleteScenicSpotsBySpotIds(Integer[] spotIds)
    {
        return scenicSpotsMapper.deleteScenicSpotsBySpotIds(spotIds);
    }

    /**
     * 删除景点信息信息
     * 
     * @param spotId 景点信息主键
     * @return 结果
     */
    @Override
    public int deleteScenicSpotsBySpotId(Integer spotId)
    {
        return scenicSpotsMapper.deleteScenicSpotsBySpotId(spotId);
    }

    @Override
    public int deleteSpotsAndStore(Long spotId, Long storeId) {
        try{
            return scenicSpotsMapper.deleteSpotsAndStore(spotId,storeId);
        }catch (Exception e){
            return 0;
        }
    }

    @Override
    public int addSpotsAndStore(Long spotId, Long storeId) {
        return scenicSpotsMapper.selectSpotsAndStore(spotId, storeId) > 0 ? 0 : scenicSpotsMapper.addSpotsAndStore(spotId, storeId);

    }
}
